package de.carcophan.jimdb.gwt.client;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

import de.carcophan.jimdb.gwt.client.presenter.Presenter;
import de.carcophan.jimdb.gwt.client.presenter.SearchPresenterImpl;
import de.carcophan.jimdb.gwt.client.service.LoginServiceAsync;
import de.carcophan.jimdb.gwt.client.service.MovieServiceAsync;
import de.carcophan.jimdb.gwt.client.view.SearchWidgetImpl;
import de.carcophan.jimdb.gwt.shared.dto.UserDTO;

public class AppController implements Presenter {

	private MovieServiceAsync movieService;
	private HasWidgets rootPanel;
	private ClientFactoryImpl clientFactory;
	private LoginServiceAsync loginService;

	public AppController() {
		this.clientFactory = new ClientFactoryImpl();
		this.movieService = clientFactory.getMovieService();
		this.loginService = clientFactory.getLoginService();
		bind();
	}

	private void bind() {
	}

	@Override
	public void go(HasWidgets panel) {
		this.rootPanel = panel;
		rootPanel.clear();
		loginService.login("admin", "123456", new AsyncCallback<UserDTO>() {
			@Override
			public void onSuccess(UserDTO user) {
				SearchPresenterImpl.Display searchWidget = new SearchWidgetImpl();
				Presenter searchPresenter = new SearchPresenterImpl(
						clientFactory, searchWidget);
				searchPresenter.go(rootPanel);
			}

			@Override
			public void onFailure(Throwable t) {
				Window.alert(t.getMessage());
			}
		});

	}

}
